<template>
  <div class="login_container">
    <img
      src="@/assets/images/login_containerback.png"
      class="login_containerback"
    />
    <div class="login_title">
      <!-- <div class="l">虚拟仿真平台管理端</div>
      <span>管理端</span> -->
      <img src="@/assets/images/logotitle.png" alt="" />
    </div>
    <div class="login_form">
      <div class="login_formtitle"> 登录 </div>
      <div class="login_formcont">
        <a-form
          ref="loginForm"
          :rules="loginRules"
          :model="userInfo"
          layout="vertical"
        >
          <a-form-item field="username" hide-label>
            <a-input
              v-model="userInfo.username"
              size="large"
              placeholder="请输入用户名"
            >
              <template #prefix>
                <icon-user />
              </template>
            </a-input>
          </a-form-item>
          <a-form-item field="password" hide-label>
            <a-input-password
              v-model="userInfo.password"
              @press-enter="handleCheck"
              size="large"
              placeholder="请输入密码"
              allow-clear
            >
              <template #prefix>
                <icon-lock />
              </template>
            </a-input-password>
          </a-form-item>
          <a-space :size="16" direction="vertical">
            <div class="login-form-password-actions">
              <a-checkbox
                checked="rememberPassword"
                :model-value="loginConfig.rememberPassword"
                @change="setRememberPassword as any"
              >
                记住密码
              </a-checkbox>
            </div>
            <a-button
              type="primary"
              @click="handleCheck"
              class="login_formbtn"
              long
              :loading="loading"
            >
              登录
            </a-button>
          </a-space>
        </a-form>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { useRouter } from 'vue-router';
  import { ValidatedError } from '@arco-design/web-vue/es/form/interface';
  import { useStorage } from '@vueuse/core';
  import { useUserStore } from '@/store';
  import useLoading from '@/hooks/loading';

  const router = useRouter();
  const { loading, setLoading } = useLoading();
  const userStore = useUserStore();

  const loginConfig = useStorage('login-config', {
    rememberPassword: false,
    username: '',
    password: '',
  });
  const userInfo = reactive({
    username: loginConfig.value.username,
    password: loginConfig.value.password,
  });

  const loginRules = {
    username: [
      {
        required: true,
        message: '请输入用户名',
      },
    ],
    password: [
      {
        required: true,
        message: '请输入密码',
      },
    ],
  };
  const loginForm = ref();
  const handleCheck = () => {
    // 下面真正登录代码;

    if (loading.value) return;
    loginForm.value.validate(
      async (errors: undefined | Record<string, ValidatedError>) => {
        if (!errors) {
          setLoading(true);
          try {
            await userStore.login(userInfo);
            const { redirect, ...othersQuery } =
              router.currentRoute.value.query;
            router.push({
              name: 'imgadmin',
              query: {
                ...othersQuery,
              },
            });
            // 实际生产环境需要进行加密存储。
            loginConfig.value.username = loginConfig.value
              ? userInfo.username
              : '';
            loginConfig.value.password = loginConfig.value
              ? userInfo.password
              : '';
          } catch (err) {
            console.log(err);
          } finally {
            setLoading(false);
          }
        }
      }
    );
  };
  const setRememberPassword = (value: boolean) => {
    loginConfig.value.rememberPassword = value;
  };
</script>
